package com.example.blog.controller;

import com.example.blog.response.*;
import com.example.blog.dto.QueryUserReqDto;
import com.example.blog.dto.QueryUserResDto;
import com.example.blog.dto.UpdateUserReqDto;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.dto.AddUserReqDto;
import com.example.blog.service.LiveUserService;
import com.example.blog.util.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Admin
 */
@RestController
@Api(value = "用户信息接口", tags = "用户信息接口")
@RequestMapping("/liveUser")
public class LiveUserController {

    @Resource
    private LiveUserService userService;

    @PostMapping("/addUser")
    @ApiOperation("添加用户信息接口")
    public ResponseModelDto addUser(@RequestBody @Validated AddUserReqDto reqDto){
        if(!userService.administrator(RequestUtil.getUserId())){
            return ResponseModels.noPowerException();
        }
        if(userService.addUser(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("添加用户失败");
        }
    }

    @PostMapping("/updateUser")
    @ApiOperation("修改用户信息")
    public ResponseModelDto updateUser(@RequestBody @Validated UpdateUserReqDto reqDto){
        if(!userService.administrator(RequestUtil.getUserId())){
            return ResponseModels.noPowerException();
        }
        if(userService.updateUser(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("修改用户失败");
        }
    }

    @PostMapping("/deleteUser")
    @ApiOperation("删除用户信息")
    public ResponseModelDto deleteUser(@RequestBody @Validated IdRequestDto reqDto){
        if(!userService.administrator(RequestUtil.getUserId())){
            return ResponseModels.noPowerException();
        }
        if(userService.deleteUser(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("删除用户失败");
        }
    }

    @PostMapping("/changeUserStatus")
    @ApiOperation("修改用户状态")
    public ResponseModelDto changeUserStatus(@RequestBody @Validated IdRequestDto reqDto){
        if(!userService.administrator(RequestUtil.getUserId())){
            return ResponseModels.noPowerException();
        }
        if(userService.changeUserStatus(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("修改用户状态失败");
        }
    }

    @GetMapping("/selectUser")
    @ApiOperation("分页查询用户信息")
    public PageableResponseModelDto<QueryUserResDto> selectUser(QueryUserReqDto reqDto){
        if(!userService.administrator(RequestUtil.getUserId())){
            throw new CommonException(ResponseCode.NOPOWER.getMessage(),ResponseCode.NOPOWER.getMessage_en(),ResponseCode.NOPOWER.getErrorCode());
        }
        return ResponseModels.page2ResponseModel(userService.selectUser(reqDto));
    }


}
